/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package parsertojaklin;

import com.mysql.jdbc.Statement;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Юра
 */
public class parser {


    public static Matcher find_in_file(String reg, String file) {
        String str, allstr="";
        try
        {
            BufferedInputStream infile = new BufferedInputStream(new FileInputStream(file));
            DataInputStream dis = new DataInputStream(infile);
            while ((dis.available()!=0))
            {
                str = dis.readLine();
                allstr+=str;
            }
            infile.close();
        }
        catch (IOException e)    {        System.err.println(e);    }
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(allstr);
        //if (matcher.matches()){
            return matcher;
        //} else return null;
    }

    public static  String find_one(String reg, String file, Hashtable res, String id){
        String str, allstr="";

        try
        {
            BufferedInputStream infile = new BufferedInputStream(new FileInputStream(file));
            DataInputStream dis = new DataInputStream(infile);
            while ((dis.available()!=0))
            {
                str = dis.readLine();
                allstr+=str;
            }
            infile.close();
        }
        catch (IOException e)
        {
            System.err.println(e);
        }
            //System.out.println("12");
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(allstr);
        if (matcher.matches()){
            if (matcher.groupCount()>2)
                res.put(id, new Wares(id, matcher.group(1), matcher. group(2), "1",  "1", ""));
                //System.out.println(id+"_"+matcher.group(1)+"_"+matcher.group(2)+"_"+matcher.group(3)+"_");
            else
                res.put(id, new Wares(id, matcher.group(1), matcher.group(2), "1", "0", ""));
                //System.out.println(id+"_"+matcher.group(1)+"_"+matcher.group(2));
            return matcher.group(1);
        }
        else {

            System.out.println("");
            return "";
        }
    }

    public static  void find_cupl(String reg, String file, Hashtable res, String parent){
    String str;
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = null;
    

    try
    {
        BufferedInputStream infile = new BufferedInputStream(new FileInputStream(file));
        DataInputStream dis = new DataInputStream(infile);
        while ((dis.available()!=0))
        {
            str = dis.readLine();
            matcher = pattern.matcher(str);
            if (matcher.matches())
            {
                Group gr = new Group(matcher.group(1),matcher.group(2),parent);
                System.out.println("--"+matcher.group(1)+"_"+matcher.group(2));
                res.put(matcher.group(1), gr);
            }

        }
        infile.close();
    }
    catch (IOException e)
    {
        System.err.println(e);
    }

    }


    public static  String strfind(String reg, String str){
        //System.out.println("12");
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher(str);
    if (matcher.matches()){
        System.out.println(matcher.group(1));
        return matcher.group(1);
    }
    else return "";
    }

    public static void wares_in_dir( Statement s) throws FileNotFoundException, IOException, SQLException {
        File dir = new File("D:\\projects\\optvideo\\optvideo\\optvideo.com\\new_design\\new_kat\\katalog\\wares_group\\");
        String[] dir_list = dir.list();
        String str, file_name="katalog.php-grupp_kod=";
        Vendor vend = new Vendor();

        for (String filename :dir_list)
        //katalog.php-grupp_kod=56&otobr=1&count_fl=1&brend_from_head=JVC
        {
            str="";
            String kod = "";
            BufferedInputStream infile = new BufferedInputStream(new FileInputStream(dir+"\\"+filename));
            DataInputStream dis = new DataInputStream(infile);
            while ((dis.available()!=0))
            {
                str = dis.readLine();
                int kod_pos = str.indexOf("view.php-kod=");
                if (kod_pos>0)
                {
                    int kod_end= str.indexOf(".htm",kod_pos);
                     kod = str.substring(kod_pos+"view.php-kod=".length(),kod_end);
                    vend.id_vendor = filename.substring(file_name.length(),filename.indexOf("&otobr="));
                    vend.name_vendor = filename.substring(filename.indexOf("&brend_from_head=")+"&brend_from_head=".length(),filename.length()-4);
                    System.out.print(kod+"-");

                    s.executeUpdate("update `wares` SET `belong_groups`="+vend.id_vendor+" where `id_wares`="+kod);//, ``="+vend.name_vendor);
                }
            }
            System.out.println(filename);
            if (false)//wareses.containsKey(kod.toString()))
            {

                break;
            }
        }

    }
}
